﻿using System;
using System.Linq;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

namespace RibbonCustomization.RibbonPages
{
    public class DemoPage8
        : WebPartPage, IPostBackEventHandler
    {
        public const string TabWithControlsAndCommandsID = "Ribbon.TabWithControlsAndCommands";

        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            SPRibbonPostBackEvent eventData = SPRibbonPostBackCommand.DeserializePostBackEvent(eventArgument);
            Page.ClientScript.RegisterStartupScript(
                typeof(DemoPage8), 
                "DemoPage8Postback", 
                String.Format("alert('You posted\\n\\tevent ID: {0}\\n\\targuments:{1}\\n\\traw: {2}');", 
                    eventData.Id ?? "<notset>",
                    eventData.Arguments != null ? 
                        String.Join(",", eventData.Arguments.Select(arg => Convert.ToString(arg)).ToArray()) : 
                        "<notset>", 
                        eventArgument), 
                true);
        }

        protected override void OnPreRender(EventArgs e)
        {
            SPRibbon ribbon = SPRibbon.GetCurrent(Page);
            if (ribbon != null)
            {
                ribbon.MakeTabAvailable(TabWithControlsAndCommandsID);

                Page.ClientScript.RegisterStartupScript(
                    typeof(DemoPage8),
                    "DemoPage8",
                    ScriptLink.BuildDelayedExecutionScript(
                        "SamplePageComponentWithPostback.js",
                        "RibbonCustomization.SamplePageComponent.init") + ";", 
                    true);

                ScriptLink.RegisterScriptAfterUI(Page, "RibbonCustomization/SamplePageComponentWithPostback.js", false, true);

                RegisterCommands();
            }
            base.OnPreRender(e);
        }

        void RegisterCommands()
        {
            SPRibbonScriptManager scriptManager = new SPRibbonScriptManager();
            
            SPRibbonPostBackCommand firstCommand = new SPRibbonPostBackCommand(
                "Ribbon.TabWithControlsAndCommands.DemoGroup.MyFirstButton", this);
            SPRibbonPostBackCommand secondCommand = new SPRibbonPostBackCommand(
                "Ribbon.TabWithControlsAndCommands.DemoGroup.MySecondButton", this,
                new object[] { "arg1", "arg2" });
            SPRibbonPostBackCommand thirdCommand = new SPRibbonPostBackCommand(
                "Ribbon.TabWithControlsAndCommands.DemoGroup.MyThirdButton", this,
                "myEventID", new object[] { "arg1", "arg2" }, "return true;");

            scriptManager.RegisterHandleCommandFunction(this, "HandleSamplePageComponentWithPostbackCommand",
                new IRibbonCommand[] { firstCommand, secondCommand, thirdCommand });
        }
    }
}
